package ext.tianma.part.Interface.MDMInterface.Service;

import java.io.File;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import ext.generic.erp.common.CommonUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.part.Interface.MDMInterface.Bean.PartInformationBean;
import ext.tianma.part.Interface.MDMInterface.Bean.PartInformationSendBean;
import ext.tianma.part.Interface.MDMInterface.Bean.SendPartsListBean;
import ext.tianma.part.Interface.MDMInterface.MDMUntil.PartAttribute;
import ext.tianma.part.Interface.Until.ClassificationMDM;
import ext.tianma.util.PropertiesUtil;
import wt.fc.ObjectReference;
import wt.fc.WTObject;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.type.ClientTypedUtility;
import wt.util.WTException;

/**
 * 用于封装部件信息,进行发送json
 * 
 * @author laichao_sun
 *
 */
public class PartInformationService {

	public static String path = "codebase" + File.separator + "ext" + File.separator + "tianma" + File.separator
			+ "part" + File.separator + "config" + File.separator + "MDMAttribute.properties";

	public static boolean sendPartsService(WTObject obj, WTArrayList partList, String status)
			throws WTException, RemoteException {
		System.out.println("SLC ==进入主数据接口=start=>");

		if (obj instanceof WTPart) {
			String partType;
			PropertiesUtil ut = new PropertiesUtil(path);
			// 用于封装发送的json
			SendPartsListBean sendPartsListBean = new SendPartsListBean();
			// 用于封装单个部件信息
			PartInformationBean partInformationBean = new PartInformationBean();

			WTPart part = (WTPart) obj;
			// 名称
			partInformationBean.setPartName(part.getName());
			// 料号
			partInformationBean.setPartNumber(part.getNumber());
			// 物料类别
			partType = ClientTypedUtility.getLocalizedTypeName(part, Locale.CHINA);
			partInformationBean.setPartType(partType);
			// 状态
			partInformationBean.setStatus(status);
			// 分类路径对应的MDM的分类代码
			String classification = ClassificationUtil.getClassification(part);
			classification = replaceBlank(classification);
			System.out.println("SLC ==分类路径==>" + classification);
			String code = ut.getValueByKey(classification);
			if (StringUtil.isNullOrEmpty(code)) {	
				throw new WTException("MDM的分类对应的值为空!请确认!");
			}
			partInformationBean.setClassification(code);
			//分类属性
			List<HashMap<String, String>> list2 = ClassificationMDM.getClassifictionIba(ClassificationUtil.getClassificationNode(part).getName(),part);
			System.out.println("SLC ==分类属性值个数==>" +list2.size());
			Map<String, String> maps = new HashMap<String, String>();
			for (HashMap<String, String> map : list2) {
				String displayname = map.get("displayname");
				String value = map.get("value");
				String key = map.get("key");
				//System.out.println("SLC ==MDM传递的分类属性==>displayname: "+displayname+" | key:"+key+" | value:"+value);
				maps.put(key, value==null?"":value);
			}
			// 读取配置文件获取属性,取部件属性值
			String attributes = ut.getValueByKey(partType + ".设计视图属性");
			if (!StringUtil.isNullOrEmpty(attributes)) {
				String[] attribute = "".equals(attributes) || attributes == null ? null : attributes.split(";");
				for (String partAttribute : attribute) {
					String partValue = PartAttribute.getAttribute(part, partAttribute);
					maps.put(partAttribute, partValue);
				}
			}
			partInformationBean.setClassificationList(maps);

			// 遍历其他视图,进行属性取值
			List list = new ArrayList();
			for (int j = 0; j < partList.size(); j++) {
				ObjectReference partref = (ObjectReference) partList.get(j);
				Object partobj = partref.getObject();
				WTPart part2 = (WTPart) partobj;
				// 读取配置文件获取属性,取部件属性值
				String attributes2 = ut.getValueByKey(partType + ".生效视图属性");
				if (!StringUtil.isNullOrEmpty(attributes2)) {
					String[] attribute = "".equals(attributes2) || attributes2 == null ? null : attributes2.split(";");
					Map<String, String> maps2 = new HashMap<String, String>();
					for (String partAttribute : attribute) {
						String partValue = PartAttribute.getAttribute(part2, partAttribute);
						if("partTemplate".equals(partAttribute)||"defaultUnit".equals(partAttribute)||"partCategory".equals(partAttribute)) {
							if(StringUtil.isNullOrEmpty(partValue)) {
								throw new WTException("传递的物料的属性:"+partAttribute+"为空,请确认!");								
							}
						}
						maps2.put(partAttribute, partValue);
					}
					list.add(maps2);
				}
			}
			Map<String, List<Map<String, String>>> maps3 = new HashMap<String, List<Map<String, String>>>();
			maps3.put("valueList", list);
			partInformationBean.setMutiCode(maps3);
			sendPartsListBean.setParts(partInformationBean);

			PartInformationSendBean partInformationSendBean = new PartInformationSendBean();
			partInformationSendBean.setServiceInterfaceCode("ESBSDPH0005");
			partInformationSendBean.setFromSystemCode("101SDP");
			partInformationSendBean.setFromInterfaceCode("");
			partInformationSendBean.setToInterfaceCode("MDMH0001");
			partInformationSendBean.setOuZone("OU_TMSH");
			partInformationSendBean.setFactoryName("");
			partInformationSendBean.setUuid("653ab555e56909e754f0b1c359283069");
			String time = CommonUtil.getCurrentTime();
			partInformationSendBean.setRequestTime(time);
			partInformationSendBean.setUserName("");
			partInformationSendBean.setPassword("");
			partInformationSendBean.setSign("");
			partInformationSendBean.setDataType("JSON");
			partInformationSendBean.setData(sendPartsListBean);

			String result2 = JSON.toJSONString(partInformationSendBean);
			// url post发布物料信息
			boolean result = PartInformationServiceToSOA.sendPartInformationBean(result2);
			System.out.println("SLC ==调用接口返回的结果==>" + result);
			return result;
		}

		return false;
	}

	// 去除所有空白格
	public static String replaceBlank(String str) {
		String dest = null;
		if (str == null) {
			return dest;
		} else {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n|&nbsp;");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
			return dest;

		}

	}

}
